package com.linjacy.httpproject.builder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.SimpleFormatter;

public class RedisUserBuilder {

    private Map<String,Object> redisHashMap = new HashMap<>();

    public static Integer maxLoginTimes = 10;

//    public RedisUserBuilder(){
//        if(redisHashMap == null){
//            synchronized (redisHashMap){
//                if(redisHashMap == null){
//                    redisHashMap = new HashMap<>();
//                }
//            }
//        }
//    }

    public RedisUserBuilder buildLoginTime(){
        String format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
        redisHashMap.put("loginTime", format);
        return this;
    }

    public RedisUserBuilder buildToken(String token){
        redisHashMap.put("token", token);
        return this;
    }

    public RedisUserBuilder buildLoginTimesAndIncrement(){
        Integer times = 0;
        if(redisHashMap.containsKey("loginTimes")){
            times = redisHashMap.get("loginTimes") instanceof Integer?(Integer) redisHashMap.get("loginTimes") + 1:0;
        }
        redisHashMap.put("loginTimes", times);
        return this;
    }

    public Map<String, Object> build(){
        return this.redisHashMap;
    }

}
